<!--
* description: 备件奖励单财务审核
* author: xiaochengzuan
* createdDate: 2021-1-22
-->
<template>
  <section class="FinancialAuditOfSparePartsReward">
    <el-dialog
      v-dialogDrag
      :visible.sync="curPopupsVisible"
      :title="textMap[popupsState]"
      :append-to-body="true"
      width="50%"
      :close-on-click-modal="false"
      @close="closeEdit"
    >
      <dialogHeader
        slot="title"
        :title="textMap[popupsState]"
        :style-type="$store.state.app.dialogType"
      />

      <div class="app-container app-container-table">
        <one-table-template
          ref="multipleTable"
          :dynamic-components="tableComponents"
          :dynamic-api-config="apiConfig"
          :dynamic-form-fields="formField"
        />
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button
          v-for="comp in tableButtons"
          :key="comp.compKey"
          :type="comp.type"
          :icon="comp.icon"
          @click="comp.clickEvent"
        >{{ comp.text }}</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { formMixins } from '@/components/mixins/formMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import api from '@/api/pa/financialManagement'

export default {
  components: {
    OneTableTemplate,
    dialogHeader: () => import('@/components/dialogHeader')
  },

  // 组件混入对象
  mixins: [formMixins],

  props: {
    popupsVisible: { type: Boolean, default: false },
    rowData: { type: Object, default: function() { return {} } }
  },

  data() {
    const viewPageCode = 'FinancialAuditOfSparePartsReward-edit'
    return {
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: 'btnKey1',
          type: 'primary',
          size: 'small',
          icon: 'iconfont  icon-preservation',
          clickEvent: () => this.save(),
          text: this.$t('sys.button.save') /* 保存*/,
          position: 'right'
        },
        {
          compKey: 'btnKey2',
          size: 'small',
          icon: 'iconfont  icon-cancel',
          clickEvent: () => this.closeEdit(1),
          text: this.$t('sys.button.cancel') // 取消
        }
      ],

      // 动态组件-查询条件
      tableComponents:
        [
          {
            compKey: 'compKey1',
            labelName: '财务审核意见：',
            codeField: 'fiReviewOpinions',
            component: () => import('@/components/org/commonInput/textareaInput'),
            type: 'textarea',
            isRequire: true,
            isMust: true,
            span: 24
          }
        ],

      // 标题
      textMap: {
        edit: '财务审核意见',
        add: '财务审核意见'
      },

      // 表单数据（无需赋值，由混入对象赋值）
      formField: this.$utils.getFormField(this, {
        fiReviewOpinions: ''
      }, this.$ConfigCache.CacheConfig.cacheData[viewPageCode])
    }
  },

  created() {
  },

  methods: {
    closeEdit() {
      this.$emit('update:popupsVisible', false)
    },

    // 驳回/通过保存按钮
    save() {
      const that = this
      this.$utils.validataMoth(this, 'validpage')
      if (this.valid) {
        if (that.popupsState === 'edit') {
          that.rejectSave()
        } else {
          that.passSave()
        }
      }
    },

    // 驳回
    rejectSave() {
      // const that = this
      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: api.editBonusFinReviewReject,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: {
          bonusOrderStatus: this.$parent.selectData.reduce((pre, cur, idx) => {
            const { bonusOrderId,
              updateControlId } = cur
            pre.push({ bonusOrderId,
              updateControlId,
              fiReviewOpinions: this.formField.fiReviewOpinions
            })
            return pre
          }, [])
        }
      }

      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then(response => {
        if (response.result === '1') {
          this.$message({
            message: response.msg,
            type: 'success',
            duration: 1000
          })
          this.$emit('update:popupsVisible', false)
          this.$nextTick(() => {
            this.$parent.queryTable(1)
          })
        } else {
          this.$message({
            message: response.msg,
            type: 'warn',
            duration: 1000
          })
        }
      })
    },

    // 通过
    passSave() {
      // const that = this
      const queryObj = {
        // 保存mutation
        type: 'mutation',
        // api配置
        apiConfig: api.editBonusFinReviewThrough,
        // 条件/实体参数（变量），根据typeParam中的定义配置
        variables: {
          bonusOrderStatus: this.$parent.selectData.reduce((pre, cur, idx) => {
            const { bonusOrderId,
              updateControlId, dlrId, bonusAmount } = cur
            pre.push({ bonusOrderId,
              updateControlId, dlrId, bonusAmount,
              fiReviewOpinions: this.formField.fiReviewOpinions
            })
            return pre
          }, [])
        }
      }
      // 转换了中台请求格式数据
      var paramD = this.$getParams(queryObj)
      // 调用中台API方法（可复用）
      this.$requestAPI(paramD).then(response => {
        if (response.result === '1') {
          this.$message({
            message: response.msg,
            type: 'success',
            duration: 1000
          })
          this.$emit('update:popupsVisible', false)
          this.$nextTick(() => {
            this.$parent.$refs.multipleTable.queryTable(1)
          })
        } else {
          this.$message({
            message: response.msg,
            type: 'warn',
            duration: 1000
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>

